<?php
/**
 * Groups module for XOOPS
 *
 * You may not change or alter any portion of this comment or credits
 * of supporting developers from this source code or any supporting source code 
 * which is considered copyrighted (c) material of the original comment or credit authors.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @copyright       The Groups Project http://code.google.com/p/groupsforxoops/
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
 * @package         groups
 * @since           2.3.0
 * @author          Hui Xiao < xh.8326@gmail.com >
 * @version         $Id: functions.uploader.php Sat Jul 18 13:08:41 CST 2009
 */

if (false === defined("XOOPS_ROOT_PATH")) {exit();}

include_once XOOPS_ROOT_PATH."/class/uploader.php";

class filesUploader extends XoopsMediaUploader {
    function getUploadDir(){
        return $this->uploadDir;
    }
    
    function mkUploadsDir($dirname="groups", $pathupload = null ){
        $pathupload = !empty($pathupload) ? $pathupload : XOOPS_UPLOAD_PATH;
        $dirname = !empty($dirname) ? $dirname : "groups";
        $path_upload = $pathupload."/".$dirname;		
		if ( !is_dir($path_upload) ) {
			if ( !@mkdir($path_upload, 0777) ){
				return false ;
			} else {
				@chmod($path_upload, 0777);
			}
		}
		$dirnameupload = date("Ymd");
		$path_upload = $path_upload . "/" .$dirnameupload;
		if ( !is_dir($path_upload) ) {
			if ( !@mkdir($path_upload, 0777) ){
				return false ;
			} else {
				@chmod($path_upload, 0777);
			}
		}
		$uploadsDir = array("path"=>$path_upload,"dirname"=>$dirnameupload);
        return $uploadsDir;
    }
	
	function uploadFile($path_upload=null,$num=0,$prefix="") {
	    if ( null == $path_upload ) return false;
	    $prefix = empty($prefix) ? date("Y_m_d") : $prefix ;
 		$media_name = $_POST["xoops_upload_file"][$num] ;
		$file_maxsize = $_FILES[$media_name]["size"];
		if ( $_POST["MAX_FILE_SIZE"] < $file_maxsize )	 {
			return false;
		}
		$allow_filetype = include "mimetypes.inc.php";
	    $uploader = new XoopsMediaUploader($path_upload, $allow_filetype, $file_maxsize);
	    if ($uploader->fetchMedia($media_name)) {
	    	$uploader->setPrefix($prefix."_");
	    	if ($uploader->upload()) {
	    		return $uploader;
	    	}
	    	return false;        
	    }
	    return false;
	}
}